﻿

using ImTools;
using Prism.Commands;
using Prism.Mvvm;
using System;
using ZDK.Infra.Helpers;
using ZDK.Models.Packages;
using ZDK.Utilities;

namespace ZDK.ViewModels
{
    public class DummyViewModel : BindableBase
    {
        private string inputFrames;
        public string InputFrames { get => inputFrames; set => SetProperty(ref inputFrames, value); }

        private string outputStr;
        public string OutputStr { get => outputStr; set => SetProperty(ref outputStr, value); }

        private string inputCrcFrames;
        public string InputCrcFrames { get => inputCrcFrames; set => SetProperty(ref inputCrcFrames, value); }

        private string outputCrc;
        public string OutputCrc { get => outputCrc; set => SetProperty(ref outputCrc, value); }
        private int inputFn;
        public int InputFn { get => inputFn; set => SetProperty(ref inputFn, value); }
        private string outputFn;
        public string OutputFn { get => outputFn; set => SetProperty(ref outputFn, value); }


        public DummyViewModel()
        {
            
        }

        private DelegateCommand parseCmd;
        public DelegateCommand ParseCmd => parseCmd ?? (parseCmd = new DelegateCommand(Parse));
        void Parse() 
        {
            if (!string.IsNullOrEmpty(InputFrames))
            {
                byte[] pkg = StrHelper.StrToHexByte(InputFrames);
                string s = "";

                //校验报文
                bool bOk = PktUtils.ValidPkg(pkg, ref s);
                if (!bOk)
                {
                    OutputStr = s;
                    return;
                }

                BasePkt pkt = null;
                s = PktUtils.TranslatePkg(pkg, ref pkt);
                if (pkt == null) OutputStr = s;
                else
                {
                    OutputStr = $"报文解析→终端id：{pkt.CtrlField.TerminalID}，终端类型：{pkt.CtrlField.TerminalType}H，{(pkt.CtrlField.Dir == 0 ? "下行报文" : "上行报文")}，来自{(pkt.CtrlField.PRM == 0 ? "从动站" : "启动站")}，{(pkt.CtrlField.ACD == 0 ? "无事件" : "有事件")}，{(pkt.CtrlField.EncryptionType == 0 ? "不加密" : "加密")}，版本{pkt.CtrlField.Version}，帧流水号：{pkt.CtrlField.FrameSerialNum}，功能码：{pkt.Afn:X2}H，{(pkt.SEQ_TpV == 0 ? "无Tp" : "有Tp")}，{pkt.SEQ_Flag}，{(pkt.SEQ_CON == 0 ? "无需" : "需")}确认回复，Fn：{pkt.Fn}，Pn：{pkt.Pn}，时间：{pkt.TimeStr} \r\n{s}";
                }  
            }
        }

        private DelegateCommand parseCrcCmd;
        public DelegateCommand ParseCrcCmd => parseCrcCmd ?? (parseCrcCmd = new DelegateCommand(ParseCrc));
        void ParseCrc() 
        {
            if (!string.IsNullOrEmpty(InputCrcFrames))
            {
                byte[] pkg = StrHelper.StrToHexByte(InputCrcFrames);
                long crcnum = CRCHelper.GetCRC16(pkg, 0, pkg.Length);
                byte b1 = (byte)(crcnum % 256);
                byte b2 = (byte)(crcnum / 256);

                OutputCrc = $"{b1.ToString("X2")} {b2.ToString("X2")}";
            }
        }

        private DelegateCommand cvtFnToHexCmd;
        public DelegateCommand CvtFnToHexCmd => cvtFnToHexCmd ?? (cvtFnToHexCmd = new DelegateCommand(CvtFnToHex));
        void CvtFnToHex() 
        {
            var fnBytes = PktUtils.transFn(InputFn);
            OutputFn = fnBytes[0].ToString("x2") + fnBytes[1].ToString("x2");
        }

    }
}
